<?php
namespace app\admin\controller;
use app\model\User as UserModel;
use think\Db;


class Index extends Base
{
    public function index()
    {
        $page_info = $this->page_info;
        $page_info['title'] = '首页';

        $menu_list = Db::name('menu')->where([
            ['pid','=',0],
            ['group','in',[0,10]]
        ])->select();
        foreach ($menu_list as $k => $vo) {
            if (!empty($vo['role']) && !roleCheck(explode(',',$vo['role']))) {
                unset($menu_list[$k]);
                continue;
            }
            $childs = Db::name('menu')->where([
                ['pid','=',$vo['id']]
            ])->select();
            foreach ($childs as $child_k => $child_v) {
                if (!empty($child_v['role']) && !roleCheck(explode(',',$child_v['role']))) {
                    unset($childs[$child_k]);
                    continue;
                }
                $childs[$child_k]['childs'] = Db::name('menu')->where([
                    ['pid','=',$child_v['id']]
                ])->select();
            }
            $menu_list[$k]['childs'] = $childs;
        }

        return view('index', [
            'page_info'=>$page_info,
            'menu_list'=>$menu_list
        ]);
    }

    public function index2()
    {
        $page_info = $this->page_info;
        $page_info['title'] = '首页';

        $menu_list = Db::name('menu')->where([
            ['pid','=',0],
            ['group','in',[1,10]]
        ])->select();
        foreach ($menu_list as $k => $vo) {
            if (!empty($vo['role']) && !roleCheck(explode(',',$vo['role']))) {
                unset($menu_list[$k]);
                continue;
            }
            $childs = Db::name('menu')->where([
                ['pid','=',$vo['id']]
            ])->select();
            foreach ($childs as $child_k => $child_v) {
                if (!empty($child_v['role']) && !roleCheck(explode(',',$child_v['role']))) {
                    unset($childs[$child_k]);
                    continue;
                }
                $childs[$child_k]['childs'] = Db::name('menu')->where([
                    ['pid','=',$child_v['id']]
                ])->select();
            }
            $menu_list[$k]['childs'] = $childs;
        }

        return view('index2', [
            'page_info'=>$page_info,
            'menu_list'=>$menu_list
        ]);
    }

    public function main(){
        $curdate = strtotime(date('Y-m-01'));
        $data = [
            'image_total'=>Db::name('image')->count(),
            'image_success'=>Db::name('image')->where('status',1)->count(),
            'image_fail'=>Db::name('image')->where('status',-1)->count(),
        ];

        return view('main', [
            'data'=>$data
        ]);
    }

    // 每日消息数
    public function mrxxs(){
        $labels = [];
        $data = [];

        $start_time = strtotime('-7 days');
        $weeks = ['周日','周一','周二','周三','周四','周五','周六'];
        for($i = 1; $i <= 7; $i++){
            $start_time = $start_time + (3600 * 24);
            $next_time = $start_time + (3600 * 24) - 1;

            $label = date('w',$start_time);
            $labels[] = $weeks[$label];

            $data[] = Db::name('msg')->where('ctime','between',[$start_time,$next_time])->count();
        }
        return json(['code'=>2000,'data'=>['labels'=>$labels,'data'=>$data]]);
    }

    // 每日活跃数
    public function mrhyrs(){
        $labels = [];
        $data = [];

        $start_time = strtotime('-7 days');
        $weeks = ['周日','周一','周二','周三','周四','周五','周六'];
        for($i = 1; $i <= 7; $i++){
            $start_time = $start_time + (3600 * 24);
            $next_time = $start_time + (3600 * 24) - 1;

            $label = date('w',$start_time);
            $labels[] = $weeks[$label];

            $data[] = Db::name('msg')->where('ctime','between',[$start_time,$next_time])->count('DISTINCT user_id');
        }
        return json(['code'=>2000,'data'=>['labels'=>$labels,'data'=>$data]]);
    }

    public function prompt($msg=''){
        return view('prompt', [
            'msg'=>$msg,
        ]);
    }

    

   
}
